package com.uzai.console.web.wechatprivate;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.constant.CacheKey;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.wechatprivate.wxcontacts.*;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.wechatprivate.WxContactsGroupSendMsgService;
import com.uzai.console.service.wechatprivate.WxContactsService;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.exportinfo.ExportAmountVo;
import com.uzai.console.vo.wechatprivate.wxcontacts.WxContactsBatchRefreshVo;
import com.uzai.console.vo.wechatprivate.wxcontacts.WxContactsInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 微信通讯录
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "个人微信微信通讯录",tags = "个人微信|微信通讯录")
@ApiSort(5)
@RestController
public class WxContactsController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(WxContactsController.class);

    @Autowired
    private WxContactsService wxContactsService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private WxContactsGroupSendMsgService wxContactsGroupSendMsgService;
    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation(value = "查询微信通讯录列表", notes = "查询微信通讯录列表", response = WxContactsInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/wxContacts/findWxContactsList")
    public Object findWxContactsList(@RequestBody WxContactsQuery  wxContactsQuery){
        wxContactsQuery.setUserType(0);//查询个人微信
        return wxContactsService.findWxContactsList(wxContactsQuery,true); //需要脱敏
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除微信通讯录", notes = "删除微信通讯录")
    @ApiOperationSupport(order = 2)
    @PostMapping("/wxContacts/deleteWxContacts")
    public Object deleteWxContacts(@RequestBody WxContactsIdListDto wxContactsIdListDto){
        wxContactsService.deleteWxContacts(wxContactsIdListDto);
        return true;
    }

    @ApiOperation(value = "查询机器人列表", notes = "查询机器人列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/wxContacts/findDeviceList")
    public Object findDeviceList(){
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
        deviceQuery.setMerId(getLoginInfo().getMerId());
        return deviceService.findDeviceList(deviceQuery);
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.SENDMESSAGE)
    @ApiOperation(value = "发送消息", notes = "发送消息")
    @ApiOperationSupport(order = 4)
    @PostMapping("/wxContacts/sendMessage")
    public Object sendMessage(@RequestBody WxContactsSendMessageDto wxContactsSendMessageDto){
        wxContactsService.sendMessage(wxContactsSendMessageDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.REGISTER)
    @ApiOperation(value = "注册", notes = "注册")
    @ApiOperationSupport(order = 5)
    @PostMapping("/wxContacts/register")
    public Object register(@RequestBody WxContactsIdDto wxContactsIdDto){
        wxContactsService.register(wxContactsIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "查询结果增加群发消息任务(方法已过时)", notes = "查询结果增加群发消息任务(方法已过时)")
    @ApiOperationSupport(order = 5)
    @PostMapping("/wxContacts/addBatchSendMessage")
    public Object addBatchSendMessageTask(@RequestBody AddBatchSendMessageTaskDto addBatchSendMessageTaskDto){
        wxContactsGroupSendMsgService.addBatchSendMessageTask(addBatchSendMessageTaskDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "同步微信通讯录", notes = "同步微信通讯录")
    @ApiOperationSupport(order = 6)
    @PostMapping("/wxContacts/refreshWxContacts")
    public Object refreshWxContacts(@RequestBody WxContactsRefreshDto wxContactsRefreshDto){
        wxContactsService.refreshWxContacts(wxContactsRefreshDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "批量同步微信通讯录", notes = "批量同步微信通讯录", response = WxContactsBatchRefreshVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/wxContacts/refreshWxContacts/batch")
    public Object batchRefreshWxContacts(@RequestBody WxContactsBatchRefreshDto wxContactsBatchRefreshDto){
        return wxContactsService.batchRefreshWxContacts(wxContactsBatchRefreshDto);
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "查询结果批量注册会员", notes = "查询结果批量注册会员")
    @ApiOperationSupport(order = 7)
    @PostMapping("/wxContacts/batchRegister")
    public Object batchRegister(@RequestBody WxContactsQuery wxContactsQuery){

        String val = (String) redisTemplate.opsForValue().get(String.format(CacheKey.WXCONTACTS_BATCH_REGISTER, wxContactsQuery.getMerId()));

        if (StringUtils.isNotBlank(val)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "5分钟只允许一次批量注册请求");
        }

        //往reids插入本次下载，允许5分钟下载一次，避免频繁提交
        redisTemplate.opsForValue().set(String.format(CacheKey.WXCONTACTS_BATCH_REGISTER, wxContactsQuery.getMerId()),wxContactsQuery.getMerId()+"",5, TimeUnit.MINUTES);

        wxContactsQuery.setUserType(0);//个人微信
        wxContactsService.batchRegisterSync(wxContactsQuery);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "新建群发消息任务", notes = "新建群发消息任务")
    @ApiOperationSupport(order = 8)
    @PostMapping("/wxContacts/newBatchSendMessageTask")
    public Object newBatchSendMessageTask(@RequestBody NewBatchSendMessageTaskDto newBatchSendMessageTaskDto){
        wxContactsGroupSendMsgService.newBatchSendMessageTask(newBatchSendMessageTaskDto, getLoginInfo().getLoginName());
        return true;
    }

    @ApiOperation(value = "查询结果导出文件前检验接口", notes = "查询结果导出文件前检验接口", response = ExportAmountVo.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/ladderRatio/getExportAmount")
    public Object getExportAmount(@RequestBody WxContactsQuery  wxContactsQuery){
        return wxContactsService.getExportAmount(wxContactsQuery);
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.EXPORT)
    @ApiOperation(value = "查询结果导出文件", notes = "查询结果导出文件")
    @ApiOperationSupport(order = 10)
    @PostMapping("/wxContacts/exportByQeuryResult")
    public Object exportByQeuryResult(@RequestBody ExportResultDto exportResultDto){
        wxContactsGroupSendMsgService.exportByQeuryResult(exportResultDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "批量同步微信备注到用户列表", notes = "批量同步微信备注到用户列表")
    @ApiOperationSupport(order = 11)
    @PostMapping("/wxContacts/batchRefreshUserRemark")
    public Object batchRefreshUserRemark(@RequestBody WxContactsBatchRefreshUserRemarkDto WxContactsBatchRefreshUserRemarkDto){
        if(WxContactsBatchRefreshUserRemarkDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }
        wxContactsService.batchRefreshUserRemark(WxContactsBatchRefreshUserRemarkDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatWxContacts, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "查询结果批量删除通讯录(只删除ES数据，不真删除关系)", notes = "查询结果批量删除通讯录(只删除ES数据，不真删除关系)")
    @ApiOperationSupport(order = 12)
    @PostMapping("/wxContacts/batchDelete")
    public Object batchDelete(@RequestBody WxContactsQuery wxContactsQuery){
        wxContactsQuery.setUserType(0);//个人微信
        wxContactsService.batchDeleteSync(wxContactsQuery);
        return true;
    }

}
